package com.bcu.calculus.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.bcu.calculus.entity.OpenIdJson;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;

public class OpenId {
    private static final String appID = "wx7182e61bedd7693d";
    private static final String appSecret = "4e47e3576cfa282959a11832f59fa755";


    public static String userLogin(@RequestParam("code") String code) throws IOException {
        String result = "";
        try {//请求微信服务器，用code换取openid。HttpUtil是工具类，后面会给出实现，Configure类是小程序配置信息，后面会给出代码
            result = com.bcu.calculus.utils.HttpUtil.doGet(
                    "https://api.weixin.qq.com/sns/jscode2session?appid="
                            + appID + "&secret="
                            + appSecret + "&js_code="
                            + code
                            + "&grant_type=authorization_code", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        OpenIdJson openIdJson = mapper.readValue(result, OpenIdJson.class);
        System.out.println(result.toString());
        System.out.println(openIdJson.getOpenid());
        return result;
    }
}
